package com.tianzj.mvc.system.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.tianzj.mvc.system.dao.SysUserMapper;
import com.tianzj.mvc.system.pojo.SysUser;
import com.tianzj.mvc.system.pojo.SysUserExample;
import com.tianzj.mvc.system.service.SysUserService;
import com.tianzj.util.MD5Utils;
import com.tianzj.util.Page;
import com.tianzj.util.SnowflakeIdWorker;

@Service
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	private SysUserMapper sysUserMapper;
	
	public SysUserExample getExample(SysUser user){
		SysUserExample example=new SysUserExample();
		SysUserExample.Criteria criteria = example.createCriteria();
		if(user.getId()!=null){
			criteria.andIdEqualTo(user.getId());
		}
		if(StringUtils.isNotEmpty(user.getUserName())){
			criteria.andUserNameEqualTo(user.getUserName());
		}
		if(StringUtils.isNotEmpty(user.getPassword())){
			criteria.andPasswordEqualTo(user.getPassword());
		}
		if(user.getStatus()!=null){
			criteria.andStatusEqualTo(user.getStatus());
		}else{
			criteria.andStatusNotEqualTo(9);
		}
		return example;
	}
	
	@Override
	public List<SysUser> getUserList(Page page, SysUser user) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		SysUserExample example=getExample(user);
		return sysUserMapper.selectByExample(example);
	}

	@Override
	public List<SysUser> getByNameAndPass(String userName, String password) {
		SysUser user = new SysUser();
		user.setUserName(userName);
		user.setPassword(password);
		SysUserExample example=getExample(user);
		return sysUserMapper.selectByExample(example);
	}

	@Override
	public void addSysUser(SysUser user) {
		user.setId(new SnowflakeIdWorker(0, 0).nextId());
		user.setPassword(MD5Utils.MD5(user.getPassword()));
		user.setStatus(SysUser.STATUS_NORMAL);
		user.setCreateDate(System.currentTimeMillis());
		sysUserMapper.insertSelective(user);
	}

	@Override
	public void deleteSysUserById(String ids) {
		String[] idArray = ids.split(",");
		for (String id : idArray) {
			SysUser sysUser = sysUserMapper.selectByPrimaryKey(Long.parseLong(id));
			sysUser.setStatus(9);
			sysUserMapper.updateByPrimaryKeySelective(sysUser);
		}
	}

	@Override
	public SysUser getSysUserById(Long id) {
		return sysUserMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateUser(SysUser sysUser) {
		sysUser.setUpdateDate(System.currentTimeMillis());
		sysUserMapper.updateByPrimaryKeySelective(sysUser);
	}

	@Override
	public SysUser getByUserNameAndPass(SysUser user) {
		List<SysUser> selectByExample = sysUserMapper.selectByExample(getExample(user));
		if(selectByExample!=null&&selectByExample.size()!=0){
			return selectByExample.get(0);
		}else{
			return null;
		}
	}

}
